package com.aiss.ws;

import android.app.Activity;
import android.app.Application;
import android.content.Context;


import com.aiss.ws.activity.BaseActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by chenqiang on 15/12/26.
 */
public class MyApplication extends Application {

    private static MyApplication instance;
    private static Context context;

    public static MyApplication getInstance() {
        if (instance == null) {
            instance = new MyApplication();
        }
        return instance;

    }

    public static Context getContext(){
        context = instance;
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    private List<BaseActivity> activityList = new LinkedList<>();

    private BaseActivity currentActivity = null;

    // 添加Activity到容器中
    public void addActivity(BaseActivity activity) {
        currentActivity = activity;
        activityList.add(activity);
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

    //从集合中移除Activity
    public void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    // 遍历所有Activity并finish
    public void exit() {
        try {
            for (Activity activity : activityList) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
}
